package org.shop.controller.supplier;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.shop.entity.Orders;
import org.shop.entity.Supplier;
import org.shop.entity.SystemMessage;
import org.shop.entity.security.MyUserDetail;
import org.shop.service.OrdersService;
import org.shop.service.SupplierService;
import org.shop.service.SystemMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("supplier")
public class SupplierOrderController {

	private static Logger logger = LogManager.getLogger(SupplierOrderController.class);
	
	@Autowired
    private OrdersService ordersService;
	@Autowired
	private SupplierService supplierService;
	@Autowired
	private SystemMessageService systemMessageService;
 	/**
	 * 店铺获取所有订单
	 * @return
	 */
	@RequestMapping("getSupplierOrderList")
	@ResponseBody
	public Map<String, Object> getShopOrderList(HttpServletRequest req){
		Map<String, Object> map = new HashMap<String, Object>();
        String currentPage = req.getParameter("offset");
        String pageSize = req.getParameter("limit");
        String orderNo = req.getParameter("orderNo");
        String userName = req.getParameter("userName");
        String userPhone = req.getParameter("userPhone");
        String goodsName = req.getParameter("goodsName");
        String orderStatus = req.getParameter("orderStatus");
        String startTime = req.getParameter("startTime");
        String endTime = req.getParameter("endTime") == null|| "".equals(req.getParameter("endTime")) ? "" : req.getParameter("endTime")+"23:59:59";
		//获取登录人用户信息
		MyUserDetail ud = (MyUserDetail) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		try {
			Long userId=ud.getUserId();
			Supplier supplier=supplierService.findSupplierByUserId(userId);
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("startPage", (Integer.parseInt(currentPage) - 1) * Integer.parseInt(pageSize));
            paramMap.put("pageSize", Integer.parseInt(pageSize));
            paramMap.put("supplierId", supplier.getSupplierId());
            paramMap.put("orderNo", orderNo);
            paramMap.put("userName", userName);
            paramMap.put("goodsName", goodsName);
            paramMap.put("userPhone", userPhone);
            paramMap.put("orderStatus", orderStatus);
            paramMap.put("startTime", startTime);
            paramMap.put("endTime", endTime);
            map = ordersService.getSupplierOrderList(paramMap);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e);
        }
		return map;
	}

	/**
	 * 商家发送订单
	 * @return
	 */
	@RequestMapping("sendOrder")
	@ResponseBody
	public Map<String,Object> sendOrder(HttpServletRequest req){
		Map<String, Object> map=new HashMap<String, Object>();
        String orderId = req.getParameter("orderId");
        String orderunique = req.getParameter("orderunique");
        MyUserDetail ud = (MyUserDetail) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		//获取登录人用户信息
		try {
			Orders orders = ordersService.selectByPrimaryKey(Long.parseLong(orderId));
			if(orders==null){
				map.put("state", 0);
				map.put("msg", "发货失败");
				return map;
			}
			orders.setOrderunique(orderunique);
			orders.setOrderStatus((byte)4);
			ordersService.updateByPrimaryKeySelective(orders);
			map.put("state", 1);
			map.put("msg", "发货成功");
			SystemMessage system = new SystemMessage();
			system.setOrderId(Long.parseLong(orderId));
			system.setSendId(ud.getUserId());
			system.setMessageType((byte) 2);
			system.setreceiveId(orders.getUserId());
			system.setMessageContent("您的订单号为"+orders.getOrderNo()+"的商品已发货,快递单号为："+orderunique+",请注意查收。");
			systemMessageService.insertSelective(system);
		} catch(Exception e) {
			map.put("state", 0);
			map.put("msg", "发货失败");
            logger.error(e);
        }
		return map;
	}
	
	
	
}
